Look.prototype._PORTAL_DOMAIN = "/rest";
Look.prototype._APP = look.getHashParam("appid") + "-" + look.getHashParam("server_id");
Look.prototype._CONTENT_LOADER_HTML = '<img src="images/content_loading.gif" style="display:block;margin:0 auto;" />';

$(document).ready(function(){
	try{
		$(window).hashchange(function(){
			look.loadFn(look.getHashParam("theme"), look.getHashParam("menu"));
		});
		$(window).hashchange();
	}catch(e){}
});

Look.prototype.loadFn = function(theme, menu){
    var typeArray = menu.split("_");
    look.publicInit.headerLoader();
  	if (theme == "" || theme == "activity" || theme == "strategy"){
		look.initLoader.activityStrategyLoader(theme);
	} else if (theme == "detail") {
		look.initLoader.detailLoader(menu);
	} else if (theme == "more"){
		look.initLoader.moreLoader();
	}
}

Look.prototype.publicInit = {
	headerLoader : function(){
		if($("#headerPanel").children().length == 0){
			look.loadPage("#headerPanel", "html/public/header.html");
			$.getJSON("data/json/" + look._APP + "/" + "contact.json?_=" + look.generateGuid(), function(data){
				$("#appNameCell").text(data.app_name);
				if(data.show_more == true){
					$("#navMoreSubset").show();
				}
			});
		}
		look.publicInit.menuSet();
	},
	menuSet : function(){
		if(look.getHashParam("menu")){
			$("#navPanel a").removeClass("selected").each(function(){
				var type = $(this).attr("type");
				$(this).attr("href", look.setParam([["theme", type], ["menu", type]], window.location.hash.replace(/(&file)(.)+/gi, "")));
				if(look.getHashParam("menu") == look.getStringParam("menu", $(this).attr("href"))){
					$(this).addClass("selected");
				}
			});
		} else {
			$("#navPanel a").removeClass("selected").first("a").addClass("selected");
		}
	}
}

Look.prototype.initLoader = {
	activityStrategyLoader: function(theme){
		if ($("#activityStrategyPanel").length == 0) {
			look.loadPage("#mainPanel", "html/activity_strategy.html");
		}
		$.getJSON("data/json/" + look._APP + "/" + theme + ".json?_=" + look.generateGuid(), function(data){
			var listPanel = $("#listPanel");
			$("#topPicCell").html(data.top.link != "" ? '<a href="' + data.top.link + '"><img src="' + data.top.pic_url + '" /></a>' : '<img src="' + data.top.pic_url + '" />');
			listPanel.html("");
			$.each(data.list, function(i, list){
				listPanel.prepend('<li><a href="' + look.setParam([["theme", "detail"]], window.location.hash + '&file=' + list.file) + '">' + list.title + '</a></li>');
			});
		});
		$.getJSON("data/json/" + look._APP + "/contact.json?_=" + look.generateGuid(), function(data){
			var linkSubset = $(".link_subset"),
				contractCell = $(".contract_cell");
			linkSubset.html("");contractCell.html("");
			$.each(data.list, function(i, list){
				contractCell.append('<p>' + list + '</p>');
			});
			$.each(data.button, function(i, button){
				if (button.name != "") {
					linkSubset.append('<p><a href="' + button.link + '" class="a_as_button">' + button.name + '</a></p>');
				}
			});
		});
	},
	detailLoader: function(menu){
		if ($("#detailPanel").length == 0) {
			look.loadPage("#mainPanel", "html/detail.html");
		}
		$.getJSON("data/json/" + look._APP + "/" + menu + ".json?_=" + look.generateGuid(), function(data){
			$.each(data.list, function(i, list){
				if(look.getHashParam("file") == list.file){
					$(".detail_title").text(list.title);
					$(".detail_content").html(look._CONTENT_LOADER_HTML);
					look.loadPage(".detail_content", "data/html/" + look._APP + "/" + list.file);
					$(".prev").html(data.list[i-1] != undefined ? '<a href="' + look.setParam([["file", data.list[i-1].file]], window.location.hash) + '">上一篇：' + data.list[i-1].title + '</a>' : '');
					$(".next").html(data.list[i+1] != undefined ? '<a href="' + look.setParam([["file", data.list[i+1].file]], window.location.hash) + '">下一篇：' + data.list[i+1].title + '</a>' : "");
				}
			});
		});
		//[事件]返回顶端
		$(".back_top").unbind("click").bind("click", function(){
			$("body,html").animate({
				scrollTop: 0
			}, 800);
			return false;
		});
	},
	moreLoader: function(){
		if ($("#morePanel").length == 0) {
			look.loadPage("#mainPanel", "html/more.html");
		}
		var gameCell = $("#gameCell");
		gameCell.html(look._CONTENT_LOADER_HTML);
		$.getJSON("data/json/more.json?_=" + look.generateGuid(), function(data){
			gameCell.html("");
			$.each(data.app_list, function(i, list){
				if(list.app_id == look._APP.split("-")[0]){
					$("#topPicCell").html(list.top.link != "" ? '<a href="' + list.top.link + '"><img src="' + list.top.pic_url + '" /></a>' : '<img src="' + list.top.pic_url + '" />');
					return;
				}
				gameCell.append(
					'<li class="box_h">' +
					'	<div class="game_name"><a href="' + list.app_link + '">' + list.app_name + '</a></div>' +
					'	<div class="game_pic box_flex"><a href="' + list.app_link + '"><img src="' + list.app_main_pic + '"></a></div>' +
					'	<div class="game_info box_flex">' +
					'		<div class="game_describe">' + look.getByteVal(list.app_intro, 88) + '...</div>' +
					'		<div class="game_bottom">' + 
					(list.download.ios != "" ? '<a href="' + list.download.ios + '" class="a_as_button">iOS免费下载</a>' : '') +
					(list.download.android != "" ? '<a href="' + list.download.android + '" class="a_as_button">安卓免费下载</a>' : '') +
					'			<a href="' + list.app_more + '" class="a_as_button selected">更多&gt;</a>' +
					'		</div>' +
					'	</div>' +
					'	<div class="clear"></div>' +
					'</li>'
				);
			});
		});
	}
}